<template>
  <el-card style="width: 500px">
    <el-form label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="form.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确认修改 </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Person",
  data(){
    return{
      form:{}
    }
  },
  created() {
    let id = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')).id : null;
    if(id){
      this.request.get("/user/findById/" + id).then(res => {
        if(res.code === '200'){
          this.form = res.data;
        }else{
          this.$message.error('找不到用户信息');
        }
      })
    }else{
      this.$message.error('找不到用户信息');
    }
  },
  methods:{
    save(){
      this.request.post("/user/save",this.form).then(res => {
        if(res.code === '200'){
          this.$message.success("更新成功")
          this.$emit('refreshUser');  //调用父组件的方法更新
        }else{
          this.$message.error('更新失败，请联系管理员');
        }
      })
    }
  }
}
</script>